package com.poem.springboot_rear.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poem.springboot_rear.common.LogAs;
import com.poem.springboot_rear.common.LogType;
import com.poem.springboot_rear.common.Result;
import com.poem.springboot_rear.entity.DeviceType;
import com.poem.springboot_rear.service.DeviceTypeService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：
 * 作者： 小祝小祝
 * 日期： 2023/11/20 15:25
 */
@CrossOrigin
@RestController
@RequestMapping("/deviceType")
public class DeviceTypeController {

    @Resource
    DeviceTypeService deviceTypeService;

    /**
     * 新增充电桩设备信息
     */
    @LogAs(operation = "新增充电宝信息", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody DeviceType deviceType){
        try{
            deviceTypeService.save(deviceType);
        }catch (Exception e){
            if (e instanceof DuplicateKeyException){
                return Result.error("添加失败");
            }else{
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改充电桩设备信息
     */
    @LogAs(operation = "修改充电宝信息", type = LogType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody DeviceType deviceType){
        try {
            deviceTypeService.updateById(deviceType);
        }catch (Exception e){
            if (e instanceof DuplicateKeyException){
                return Result.error("修改错误");
            }else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除单个充电桩设备信息
     */
    @LogAs(operation = "删除充电宝信息", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        deviceTypeService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除充电桩设备信息
     */
    @LogAs(operation = "批量删除充电宝信息", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        deviceTypeService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 查询单个充电桩设备信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        deviceTypeService.getById(id);
        return Result.success();
    }

    /**
     * 分页查询充电桩设备信息
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageIndex,
                               @RequestParam Integer pageSize,
                               @RequestParam String manufacturer,
                               @RequestParam String name){
        QueryWrapper<DeviceType> queryWrapper = new QueryWrapper<DeviceType>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(manufacturer),"manufacturer",manufacturer);
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name);
        Page<DeviceType> page = deviceTypeService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        return Result.success(page);
    }
}
